﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.Plumbing
{
	public partial class SetTrapForm : System.Windows.Forms.Form
	{
		public SetTrapForm()
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{C84A46BA-1778-47D5-8D7D-7A0859077B41}", null);
			this.BindCombobox();
		}

		private void BindCombobox()
		{
			this.cmbTrap.Items.Clear();
			this.cmbTrap.Items.Add("S弯");
			this.cmbTrap.Items.Add("P弯");
			this.cmbTrap.Items.Add("无");
			this.cmbTrap.SelectedIndex = int.Parse(this.userSetting.GetConnectTrapText());
		}

		private void SetTrapForm_Deactivate(object sender, EventArgs e)
		{
			this.userSetting.WriteConnectTrapText(this.cmbTrap.SelectedIndex);
		}

		private void SetTrapForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetTrapForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			SetTrapForm.SendMessage(HYPipeConnectFixtures.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			SetTrapForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SetTrapForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SetTrapForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
		}

		private void SetTrapForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private ControlInfoManager m_thisManger;
	}
}
